package com.jack;

import cn.hutool.db.sql.Order;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
@Configuration
public class SentinelMain8401 {

    public static void main(String[] args) {
        SpringApplication.run(SentinelMain8401.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }


    @RestController
    public class Controller{

        @Autowired
        public RestTemplate restTemplate;

        @GetMapping("/testA")
        public String testA(){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("test A A A " );
            String forObject = restTemplate.getForObject("http://sentinel-service-8401/testB", String.class);
            return forObject;
        }

        @GetMapping("/testB")
        public String testB(){
            return "Test B B B ";
        }

        @GetMapping("/testD")
        public String testD(){
            int age = 10 / 0;
            return "Test D D D ";
        }


        @GetMapping("/test5")
        @SentinelResource(value = "test5", blockHandler = "test5_handler") //blockHandler 配置处理
        public String test5(@RequestParam(value = "p1", required = false) String p1){
            log.info("p1 = "+ p1 );
            return "test55";
        }



        public String test5_handler(String p1 , BlockException blockE){
            return "test5_handler genus .... ";
        }


        @GetMapping("/select/{id}")
        @SentinelResource(value = "select", fallback = "fallback_select") //java内异常处理
        public Optional<Map> select(@PathVariable("id") Integer id){

            return getOrderById(id);
        }

        public Optional<Map> getOrderById(Integer id){
            //service
            Map hashMap = new HashMap();
            hashMap.put("id", id);
            hashMap.put("goods", "他司机的礼物啦");
            throw new RuntimeException("somethin wrong ...");
//            return Optional.of(hashMap);
        }

        public Optional<Map> fallback_select(@PathVariable("id") Integer id){

            Map map = new HashMap();
            map.put("id", id);
            map.put("goods", "dawdwadjwdw");
            map.put("msg", "404");
            return Optional.of(map);
        }



    }

}
